package com.example.firstopengl.objects;

import android.opengl.GLES20;

import com.example.firstopengl.data.VertexArray;
import com.example.firstopengl.programs.TextureShaderProgram;

import static com.example.firstopengl.util.Constant.BYTES_PER_FLOAT;

/**
 * Created by liqian-ps on 2017/7/6.
 */

public class Table {

    public static final int POSITION_COMPONENT_COUNT = 2;
    public static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    public static final int STRIDE = (POSITION_COMPONENT_COUNT + TEXTURE_COORDINATES_COMPONENT_COUNT)*BYTES_PER_FLOAT;

    private float[] VERTEX_DATA = {
        // X , Y , S, T
            0f, 0f,  0.5f, 0.5f,
            -0.5f, -0.8f, 0f, 0.9f,
            0.5f, -0.8f,  1f, 0.9f,
            0.5f, 0.8f,   1f, 0.1f,
            -0.5f, 0.8f,  0f, 0.1f,
            -0.5f, -0.8f, 0f, 0.9f
    };

    private VertexArray vertexArray;
    public Table(){
        vertexArray = new VertexArray(VERTEX_DATA);
    }

    public void bindData(TextureShaderProgram textureProgram){
        vertexArray.setVertexAttribPointer(0, textureProgram.getPositionAttributeLocation(), POSITION_COMPONENT_COUNT, STRIDE);
        vertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT,textureProgram.getTextureCoordinatesLocation() , TEXTURE_COORDINATES_COMPONENT_COUNT, STRIDE);
    }

    public void draw(){
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);
    }
}
